﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;

namespace Elca.MvvmHelpers {
    public class WidthConverter : IMultiValueConverter {

//        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {

            //bool toBeDisplayed = (bool)parameter;
            //if (!toBeDisplayed) {
            //    return 0.0;
            //}

//            return (double)value;
//        }

        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {

            if (values.Length != 2) {
                return null;
            }

            object o0 = values[0];
            object o1 = values[1];

            if (! (o1 is bool)) {
                return o0;
            }
            bool toBeDisplayed = (bool) o1;
            if (! toBeDisplayed) {
                return 0.0;
            }

            if (! (o0 is double)) {
                return 0;
            }

            return (double) o0;
            
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) {

            return new object[] { (double)value, Binding.DoNothing };
        }
    }
}
